
/**
 * 计算等额本息还款
 * @param principal 贷款总额
 * @param month 贷款期限
 * @param rate 贷款利率
 */
function calculateEqualPrincipalAndInterest(principal, month, rate) {
  // console.log("计算等额本息还款，本金： " + principal + "，期数： " + month + "，利率： " + rate)

  var monthRate = rate / (100 * 12);//月利率
  var preLoan = (principal * monthRate * Math.pow((1 + monthRate), month)) / (Math.pow((1 + monthRate), month) - 1);//每月还款金额;
  var totalMoney = preLoan * month;//还款总额
  var interest = totalMoney - principal;//还款总利息

  //期数 1
  //月供总额 preLoan
  //首页还款利息  principal * monthRate
  //首月还款本金  preLoan - principal * monthRate
  //剩余本金 principal-（ preLoan - principal * monthRate ）

  var arr = [];

  let temp_principal = principal;

  for(var i=0;i<month;i++){
    let obj = {};

    obj.count = i+1;
    obj.preLoan = preLoan;
    obj.interest = temp_principal * monthRate;
    obj.principal_paid = preLoan - temp_principal * monthRate;
    obj.principal_left = temp_principal - (preLoan - temp_principal * monthRate );

    temp_principal = temp_principal - obj.principal_paid;

    obj.preLoan = parseFloat(obj.preLoan).toFixed(2);
    obj.interest = parseFloat(obj.interest).toFixed(2);
    obj.principal_paid = parseFloat(obj.principal_paid).toFixed(2);
    obj.principal_left = parseFloat(obj.principal_left).toFixed(2);

      arr[i] = obj;
      
  }  
  //add 数据格式化..
  var result = {};
  result.totalMoney = totalMoney;
  result.principal = principal;
  result.interest = interest;
  result.preLoan = preLoan;
  result.month = month;
  result.detail = arr;


  result.totalMoney = parseFloat(result.totalMoney).toFixed(2);
  result.principal = parseFloat(result.principal).toFixed(2);
  result.interest = parseFloat(result.interest).toFixed(2);
  result.preLoan = parseFloat(result.preLoan).toFixed(2);
 

  return result;

}


/**
 * 计算等额本金还款
 * @param principal 贷款总额
 * @param months 贷款期限
 * @param rate 贷款利率
 */
function calculateEqualPrincipal(principal, months, rate) {
  var monthRate = rate / (100 * 12);//月利率
  var prePrincipal = principal / months;//每月还款本金
  var firstMonth = prePrincipal + principal * monthRate;//第一个月还款金额
  var decreaseMonth = prePrincipal * monthRate;//每月利息递减
  var interest = (months + 1) * principal * monthRate / 2;//还款总利息
  var totalMoney = principal + interest;//还款总额

  //期数 1
  //月供总额（首月） firstMonth - decreaseMonth*（期数-1）
  //每月递减 decreaseMonth
  //每月还款本金  principal/months
  //剩余本金 principal - principal*(i+1)/months
   //首页还款利息  principal * monthRate

  var arr = [];

  let temp_principal = principal;

  

  for (var i = 0; i < months; i++) {
    let obj = {};

    obj.count = i + 1;
    //月供总额
    obj.preLoan = firstMonth - decreaseMonth*i;
    //月供本金
    obj.principal_paid = principal / months;

    //月供利息
    obj.interest = temp_principal * monthRate;
    
    //剩余本金
    obj.principal_left = temp_principal - principal / months;

    temp_principal = temp_principal - obj.principal_paid;

    // console.log("等额本金还款  第" + obj.count + "期，每月还" + obj.preLoan + "元，其中利息" + obj.interest + "元，本月已还本金"
    //   + obj.principal_paid + "元，剩余未还本金" + obj.principal_left);


    obj.preLoan = parseFloat(obj.preLoan).toFixed(2);
    obj.principal_paid = parseFloat(obj.principal_paid).toFixed(2);
    obj.interest = parseFloat(obj.interest).toFixed(2);
    obj.principal_left = parseFloat(obj.principal_left).toFixed(2);

      arr[i] = obj;

  }  

  var result = {};
  result.totalMoney = totalMoney;
  result.principal = principal;
  result.interest = interest;
  result.firstMonth = firstMonth;
  result.decreaseMonth = decreaseMonth;
  result.month = months;
  result.detail = arr;

  return result;
}

/**
 * 一次性提前还款计算（等额本息）
 * @param principal 贷款总额
 * @param months 贷款期限
 * @param payTimes 已还次数
 * @param rate 贷款利率
 */
function calculateEqualPrincipalAndInterest1(principal, months, payTimes, rate) {

  var monthRate = rate / (100 * 12);//月利率
  var preLoan = (principal * monthRate * Math.pow((1 + monthRate), months)) / (Math.pow((1 + monthRate), months) - 1);//每月还款金额
  var totalMoney = preLoan * months;//还款总额
  var interest = totalMoney - principal;//还款总利息
  var leftLoan = principal * Math.pow(1 + monthRate, payTimes) - preLoan * (Math.pow(1 + monthRate, payTimes) - 1) / monthRate;//n个月后欠银行的钱
  var payLoan = principal - leftLoan;//已还本金
  var payTotal = preLoan * payTimes;//已还总金额
  var payInterest = payTotal - payLoan;//已还利息
  var totalPayAhead = leftLoan * (1 + monthRate);//剩余一次还清
  var saveInterest = totalMoney - payTotal - totalPayAhead;

  var result = {};
  result.monthRate = monthRate;
  result.preLoan = preLoan;
  result.totalMoney = totalMoney;
  result.interest = interest;
  result.leftLoan = leftLoan;
  result.payLoan = payLoan;
  result.payInterest = payInterest;
  result.totalPayAhead = totalPayAhead;
  result.saveInterest = saveInterest;
  result.leftMonth = 0;

  return result;


}

/**
 *  一次性提前还款计算(等额本金)
 * @param principal 贷款总额
 * @param months 贷款期限
 * @param payTimes 已还次数
 * @param rate 贷款利率
 */
function calculateEqualPrincipal1(principal, months, payTimes, rate) {

  var monthRate = rate / (100 * 12);//月利率
  var prePrincipal = principal / months;//每月还款本金
  var firstMonth = prePrincipal + principal * monthRate;//第一个月还款金额
  var decreaseMonth = prePrincipal * monthRate;//每月利息递减
  var interest = (months + 1) * principal * monthRate / 2;//还款总利息
  var totalMoney = principal + interest;//还款总额
  var payLoan = prePrincipal * payTimes;//已还本金
  var payInterest = (principal * payTimes - prePrincipal * (payTimes - 1) * payTimes / 2) * monthRate;//已还利息
  var payTotal = payLoan + payInterest;//已还总额
  var totalPayAhead = (principal - payLoan) * (1 + monthRate);//提前还款金额（剩余本金加上剩余本金当月利息）
  var saveInterest = totalMoney - payTotal - totalPayAhead;

  var result = {};
  result.principal = principal;
  result.totalMoney = totalMoney;
  result.interest = interest;
  result.firstMonth = firstMonth;
  result.decreaseMonth = decreaseMonth;
  result.payLoan = payLoan;
  result.payInterest = payInterest;
  result.saveInterest = saveInterest;
  result.leftMonth = 0;

  return result;


}

/**
 * 部分提前还款计算（等额本息、月供不变）
 * @param principal 贷款总额
 * @param months 贷款期限
 * @param aheadPrincipal 提前还款金额
 * @param payTimes 已还次数
 * @param rate 贷款利率
 */
function calculateEqualPrincipalAndInterestApart(principal, months, aheadPrincipal, payTimes, rate) {

  var monthRate = rate / (100 * 12);//月利率
  var preLoan = (principal * monthRate * Math.pow((1 + monthRate), months)) / (Math.pow((1 + monthRate), months) - 1);//每月还款金额
  var totalMoney = preLoan * months;//还款总额
  var interest = totalMoney - principal;//还款总利息
  var leftLoanBefore = principal * Math.pow(1 + monthRate, payTimes) - preLoan * (Math.pow(1 + monthRate, payTimes) - 1) / monthRate;//提前还款前欠银行的钱
  var leftLoan = principal * Math.pow(1 + monthRate, payTimes + 1) - preLoan * (Math.pow(1 + monthRate, payTimes + 1) - 1) / monthRate - aheadPrincipal;//提前还款后欠银行的钱
  var payLoan = principal - leftLoanBefore;//已还本金
  var payTotal = preLoan * payTimes;//已还总金额
  var payInterest = payTotal - payLoan;//已还利息
  var aheadTotalMoney = aheadPrincipal + preLoan;//提前还款总额

  var leftMonth = Math.floor(Math.log(preLoan / (preLoan - leftLoan * monthRate)) / Math.log(1 + monthRate));//计算剩余还款期限
  var newPreLoan = (leftLoan * monthRate * Math.pow((1 + monthRate), leftMonth)) / (Math.pow((1 + monthRate), leftMonth) - 1);//剩余贷款每月还款金额
  var leftTotalMoney = newPreLoan * leftMonth;//剩余还款总额
  var leftInterest = leftTotalMoney - (leftLoan - aheadPrincipal);
  var saveInterest = totalMoney - aheadTotalMoney - leftTotalMoney - payTotal;

  var result = {};
  result.totalMoney = totalMoney;
  result.principal = principal;
  result.interest = interest;
  result.preLoan = preLoan;
  result.payTotal = payTotal;
  result.payLoan = payLoan;
  result.payInterest = payInterest;
  result.aheadTotalMoney = aheadTotalMoney;
  result.leftTotalMoney = leftTotalMoney;
  result.leftInterest = leftInterest;
  result.newPreLoan = newPreLoan;
  result.saveInterest = saveInterest;
  result.leftMonth = leftMonth;

  return result;
}

/**
 * 部分提前还款计算(等额本金、月供不变)
 * @param principal 贷款总额
 * @param months 贷款期限
 * @param aheadPrincipal 提前还款金额
 * @param payTimes 已还次数
 * @param rate 贷款利率
 */
function calculateEqualPrincipalApart(principal, months, aheadPrincipal, payTimes, rate) {

  var monthRate = rate / (100 * 12);//月利率
  var prePrincipal = principal / months;//每月还款本金
  var firstMonth = prePrincipal + principal * monthRate;//第一个月还款金额
  var decreaseMonth = prePrincipal * monthRate;//每月利息递减
  var interest = (months + 1) * principal * monthRate / 2;//还款总利息
  var totalMoney = principal + interest;//还款总额
  var payLoan = prePrincipal * payTimes;//已还本金
  var payInterest = (principal * payTimes - prePrincipal * (payTimes - 1) * payTimes / 2) * monthRate;//已还利息
  var payTotal = payLoan + payInterest;//已还总额
  var aheadTotalMoney = (principal - payLoan) * monthRate + aheadPrincipal + prePrincipal;//提前还款金额
  var leftLoan = principal - aheadPrincipal - payLoan - prePrincipal;//剩余金额
  var leftMonth = Math.floor(leftLoan / prePrincipal);
  var newPrePrincipal = leftLoan / leftMonth;//新的每月还款本金
  var newFirstMonth = newPrePrincipal + leftLoan * monthRate;//新的第一个月还款金额
  var newDecreaseMonth = newPrePrincipal * monthRate;//新的每月利息递减
  var leftInterest = (leftMonth + 1) * leftLoan * monthRate / 2;//还款总利息
  var leftTotalMoney = leftLoan + leftInterest;//还款总额
  var saveInterest = totalMoney - payTotal - aheadTotalMoney - leftTotalMoney;

  var result = {};
  result.totalMoney = totalMoney;
  result.principal = principal;
  result.interest = interest;
  result.firstMonth = firstMonth;
  result.decreaseMonth = decreaseMonth;
  result.payTotal = payTotal;
  result.payInterest = payInterest;
  result.aheadTotalMoney = aheadTotalMoney;
  result.leftTotalMoney = leftTotalMoney;
  result.leftInterest = leftInterest;
  result.newFirstMonth = newFirstMonth;
  result.newDecreaseMonth = newDecreaseMonth;
  result.saveInterest = saveInterest;
  result.leftMonth = leftMonth;

  return result;

}

/**
 * 部分提前还款计算（等额本息、期限不变）
 * @param principal
 * @param months
 * @param aheadPrincipal
 * @param payTimes
 * @param rate
 */
function calculateEqualPrincipalAndInterestApart2(principal, months, aheadPrincipal, payTimes, rate) {

  var monthRate = rate / (100 * 12);//月利率
  var preLoan = (principal * monthRate * Math.pow((1 + monthRate), months)) / (Math.pow((1 + monthRate), months) - 1);//每月还款金额
  var totalMoney = preLoan * months;//还款总额
  var interest = totalMoney - principal;//还款总利息
  var leftLoanBefore = principal * Math.pow(1 + monthRate, payTimes) - preLoan * (Math.pow(1 + monthRate, payTimes) - 1) / monthRate;//提前还款前欠银行的钱
  var leftLoan = principal * Math.pow(1 + monthRate, payTimes + 1) - preLoan * (Math.pow(1 + monthRate, payTimes + 1) - 1) / monthRate;//提前还款后银行的钱
  var payLoan = principal - leftLoanBefore;//已还本金
  var payTotal = preLoan * payTimes;//已还总金额
  var payInterest = payTotal - payLoan;//已还利息
  var aheadTotalMoney = preLoan + aheadPrincipal;//下个月还款金额
  var newPreLoan = ((leftLoan - aheadPrincipal) * monthRate * Math.pow((1 + monthRate), months - payTimes - 1)) / (Math.pow((1 + monthRate), months - payTimes - 1) - 1);//下个月起每月还款金额
  var leftTotalMoney = newPreLoan * (months - payTimes);
  var leftInterest = leftTotalMoney - (leftLoan - aheadPrincipal);
  var saveInterest = totalMoney - payTotal - aheadTotalMoney - leftTotalMoney;

  var result = {};
  result.totalMoney = totalMoney;
  result.principal = principal;
  result.interest = interest;
  result.preLoan = preLoan;
  result.payInterest = payInterest;
  result.aheadTotalMoney = aheadTotalMoney;
  result.leftTotalMoney = leftTotalMoney;
  result.leftInterest = leftInterest;
  result.newPreLoan = newPreLoan;
  result.saveInterest = saveInterest;
  result.months = months;

  return result;

}

/**
 * 部分提前还款计算(等额本金、期限不变)
 * @param principal 贷款总额
 * @param months 贷款期限
 * @param aheadPrincipal 提前还款金额
 * @param payTimes 已还次数
 * @param rate 贷款利率
 */
function calculateEqualPrincipalApart2(principal, months, aheadPrincipal, payTimes, rate) {

  var monthRate = rate / (100 * 12);//月利率
  var prePrincipal = principal / months;//每月还款本金
  var firstMonth = prePrincipal + principal * monthRate;//第一个月还款金额
  var decreaseMonth = prePrincipal * monthRate;//每月利息递减
  var interest = (months + 1) * principal * monthRate / 2;//还款总利息
  var totalMoney = principal + interest;//还款总额
  var payLoan = prePrincipal * payTimes;//已还本金
  var payInterest = (principal * payTimes - prePrincipal * (payTimes - 1) * payTimes / 2) * monthRate;//已还利息
  var payTotal = payLoan + payInterest;//已还总额
  var aheadTotalMoney = (principal - payLoan) * monthRate + aheadPrincipal + prePrincipal;//提前还款金额
  var leftMonth = months - payTimes - 1;
  var leftLoan = principal - aheadPrincipal - payLoan - prePrincipal;
  var newPrePrincipal = leftLoan / leftMonth;//新的每月还款本金
  var newFirstMonth = newPrePrincipal + leftLoan * monthRate;//新的第一个月还款金额
  var newDecreaseMonth = newPrePrincipal * monthRate;//新的每月利息递减
  var leftInterest = (leftMonth + 1) * leftLoan * monthRate / 2;//还款总利息
  var leftTotalMoney = leftLoan + leftInterest;//还款总额
  var saveInterest = totalMoney - payTotal - aheadTotalMoney - leftTotalMoney;
  var result = {};
  result.totalMoney = totalMoney;
  result.principal = principal;
  result.interest = interest;
  result.firstMonth = firstMonth;
  result.decreaseMonth = decreaseMonth;
  result.payTotal = payTotal;
  result.payLoan = payLoan;
  result.payInterest = payInterest;
  result.newFirstMonth = newFirstMonth;
  result.newDecreaseMonth = newDecreaseMonth;
  result.saveInterest = saveInterest;
  result.leftMonth = leftMonth;

  return result;
}

/**
 * 构建传递参数
 * type, 类型 商业贷款、公积金贷款、组合贷款
 * measure, 等额本金 等额本息
 * commercialAmount, 商业贷款金额
 * commercialRatio, 商业贷款利率
 * commercialDuration,商业贷款年限
 * accumulationFundAmount, 公积金贷款金额
 * accumulationFundRatio, 公积金贷款利率
 * accumulationFundDuration 公积金贷款年限
 */
function buildParameter(type, measure, commercialAmount, commercialRatio, commercialDuration,
  accumulationFundAmount, accumulationFundRatio, accumulationFundDuration ){
  var parameter = {};
  parameter.type = type;
  parameter.measure = measure;
  parameter.commercialAmount = commercialAmount;
  parameter.commercialRatio = commercialRatio;
  parameter.commercialDuration = commercialDuration;
  parameter.accumulationFundAmount = accumulationFundAmount;
  parameter.accumulationFundRatio = accumulationFundRatio;
  parameter.accumulationFundDuration = accumulationFundDuration;
  return parameter;
}

/**
 * 进行计算...
 */
function calculate(parameter){
  //结论
  let conclusion = {};
  //等额本金
  let equivalentPrincipal = equivalentPrincipalConclusion(
    parameter.commercialAmount, 
    parameter.commercialRatio, 
    parameter.commercialDuration,
    parameter.accumulationFundAmount, 
    parameter.accumulationFundRatio, 
    parameter.accumulationFundDuration);

  //等额本息
  let equivalentPrincipalAndInterest = equivalentPrincipalAndInterestConclusion(
    parameter.commercialAmount, 
    parameter.commercialRatio, 
    parameter.commercialDuration,
    parameter.accumulationFundAmount, 
    parameter.accumulationFundRatio, 
    parameter.accumulationFundDuration);

  conclusion.equivalentPrincipal = equivalentPrincipal;
  conclusion.equivalentPrincipalAndInterest = equivalentPrincipalAndInterest;

  let isCommercialLoan = (parameter.commercialAmount != null) && (parameter.commercialRatio != null) && (parameter.commercialDuration != null);
  let isAccumulation = (parameter.accumulationFundAmount != null) && (parameter.accumulationFundRatio != null) && (parameter.accumulationFundDuration != null);

  if (isCommercialLoan && isAccumulation){
    conclusion.isPortfolioLoan = true;
  }
  return conclusion;
}

/**
 * 等额本金计算
 */
function equivalentPrincipalConclusion(commercialAmount, commercialRatio, commercialDuration,
  accumulationFundAmount, accumulationFundRatio, accumulationFundDuration){

    var conclusion = {};

    var totalLoan = 0;
    var totalInterest = 0;
    var totalYear = 0;
    var payPerMonth = 0;
    var decreasePerMonth = 0;

    var tabHeader = null;
    var tabBody = null;

  var isCommercialLoan = (commercialAmount != null) && (commercialRatio != null) && (commercialDuration != null);
  var isAccumulation = (accumulationFundAmount != null) && (accumulationFundRatio != null) && (accumulationFundDuration != null);

    tabHeader = modifyTabHeader(isCommercialLoan, isAccumulation);


  //TODO 计算、填表...
  if (isCommercialLoan && !isAccumulation) {
    let comercialConclusion = calculateEqualPrincipal(commercialAmount * 10000, commercialDuration * 12, commercialRatio);
    totalLoan = commercialAmount;
    totalInterest = comercialConclusion.interest;
    totalYear = commercialDuration;
    payPerMonth = comercialConclusion.firstMonth;
    decreasePerMonth = comercialConclusion.decreaseMonth;
    tabBody = comercialConclusion.detail;
  } 
  
  if (isAccumulation && !isCommercialLoan ) {
    let accumulationFundConclusion = calculateEqualPrincipal(accumulationFundAmount * 10000, accumulationFundDuration * 12, accumulationFundRatio);
    totalLoan = accumulationFundAmount;
    totalInterest = accumulationFundConclusion.interest;
    totalYear = accumulationFundDuration;
    payPerMonth = accumulationFundConclusion.firstMonth;
    decreasePerMonth = accumulationFundConclusion.decreaseMonth
    tabBody = accumulationFundConclusion.detail;
  }


  //todo 组合贷款...
  if (isAccumulation && isCommercialLoan){
    let comercialConclusion = calculateEqualPrincipal(commercialAmount * 10000, commercialDuration * 12, commercialRatio);
    let accumulationFundConclusion = calculateEqualPrincipal(accumulationFundAmount * 10000, accumulationFundDuration * 12, accumulationFundRatio);

    totalLoan = accumulationFundAmount + commercialAmount;
    totalInterest = accumulationFundConclusion.interest + comercialConclusion.interest;
    totalYear = accumulationFundDuration > commercialDuration ? accumulationFundDuration : commercialDuration ;
    payPerMonth = accumulationFundConclusion.firstMonth + comercialConclusion.firstMonth;
    decreasePerMonth = accumulationFundConclusion.decreaseMonth + comercialConclusion.decreaseMonth;


    // tabBody = accumulationFundConclusion.detail;

    var tbody = [];

    for (var i = 0; i < totalYear * 12; i++) {
      var obj = {}
      obj.count = i + 1;
      
      obj.comericalPay = parseFloat(comercialConclusion.firstMonth) - parseFloat(comercialConclusion.decreaseMonth)*i ;
      obj.accumulationPay = parseFloat(accumulationFundConclusion.firstMonth) - parseFloat(accumulationFundConclusion.decreaseMonth)*i;
      if (obj.comericalPay < 0 || i > commercialDuration*12 -1){
        obj.comericalPay = 0;
      }

      if (obj.accumulationPay < 0 || i > accumulationFundDuration*12 -1){
        obj.accumulationPay = 0;
      }

      obj.totalPay = obj.comericalPay + obj.accumulationPay;
      obj.totalPay = parseFloat(obj.totalPay).toFixed(2);
      obj.comericalPay = parseFloat(obj.comericalPay).toFixed(2);
      obj.accumulationPay = parseFloat(obj.accumulationPay).toFixed(2);

      tbody[i] = obj;
    }

    tabBody = tbody;


  }


  conclusion.totalLoan = totalLoan ;
  conclusion.totalInterest = totalInterest;
  conclusion.totalYear = totalYear;
  conclusion.payPerMonth = payPerMonth;
  conclusion.decreasePerMonth = decreasePerMonth;
  conclusion.tabHeader = tabHeader;
  conclusion.tabBody = tabBody;

  //todo 数据规范化
  conclusion.totalInterest = parseFloat(conclusion.totalInterest).toFixed(2);
  conclusion.decreasePerMonth = parseFloat(conclusion.decreasePerMonth).toFixed(2);
  conclusion.payPerMonth = parseFloat(conclusion.payPerMonth).toFixed(2);

  return conclusion;
}

/**
 * 等额本息计算
 */
function equivalentPrincipalAndInterestConclusion(commercialAmount, commercialRatio, commercialDuration,
  accumulationFundAmount, accumulationFundRatio, accumulationFundDuration){
  var conclusion = {};
  var totalLoan = 0;
  var totalInterest = 0;
  var totalYear = 0;
  var payPerMonth = 0;
  var tabHeader = null;
  var tabBody = null;


  var isCommercialLoan = (commercialAmount != null) && (commercialRatio != null) && (commercialDuration != null);
  var isAccumulation = (accumulationFundAmount != null) && (accumulationFundRatio != null) && (accumulationFundDuration != null);

  //TODO 计算、填表...
  tabHeader = modifyTabHeader(isCommercialLoan, isAccumulation);
  if (isCommercialLoan && !isAccumulation){
    let comercialConclusion = calculateEqualPrincipalAndInterest(commercialAmount*10000, commercialDuration*12, commercialRatio);
    totalLoan = commercialAmount;
    totalInterest = comercialConclusion.interest;
    totalYear = commercialDuration;
    payPerMonth = comercialConclusion.preLoan;
    tabBody = comercialConclusion.detail;

  } 
  
  if (isAccumulation && !isCommercialLoan){
    let accumulationConclusion = calculateEqualPrincipalAndInterest(accumulationFundAmount * 10000, accumulationFundDuration * 12, accumulationFundRatio);

    totalLoan = accumulationFundAmount;
    totalInterest = accumulationConclusion.interest;
    totalYear = accumulationFundDuration;
    payPerMonth = accumulationConclusion.preLoan;
    tabBody = accumulationConclusion.detail;
  }


  //TODO 组合贷款...
  if (isAccumulation && isCommercialLoan) {
    let comercialConclusion = calculateEqualPrincipalAndInterest(commercialAmount * 10000, commercialDuration * 12, commercialRatio);
    let accumulationConclusion = calculateEqualPrincipalAndInterest(accumulationFundAmount * 10000, accumulationFundDuration * 12, accumulationFundRatio);

    totalLoan = parseInt(commercialAmount) + parseInt(accumulationFundAmount);
    totalInterest = parseFloat(accumulationConclusion.interest) + parseFloat(comercialConclusion.interest);
    
   
    totalYear = accumulationFundDuration > commercialDuration ? accumulationFundDuration : commercialDuration ;
    payPerMonth = parseFloat(accumulationConclusion.preLoan) + parseFloat(comercialConclusion.preLoan);

    var tbody = [];

    for (var i = 0; i < totalYear*12;i++){
      var obj = {}
      obj.count = i+1;
      obj.totalPay = payPerMonth;

      

      obj.comericalPay = comercialConclusion.preLoan;
      obj.accumulationPay = accumulationConclusion.preLoan;

      if (i > commercialDuration * 12 - 1) {
        obj.comericalPay = 0;
      }

      if (i > accumulationFundDuration * 12 - 1) {
        obj.accumulationPay = 0;
      }

      obj.totalPay = parseFloat(obj.comericalPay) + parseFloat(obj.accumulationPay);

      obj.totalPay = parseFloat(obj.totalPay).toFixed(2);
      obj.comericalPay = parseFloat(obj.comericalPay).toFixed(2);
      obj.accumulationPay = parseFloat(obj.accumulationPay).toFixed(2);

      tbody[i] = obj;
    }

    tabBody = tbody;

  }


  conclusion.totalLoan = totalLoan;
  conclusion.totalInterest = totalInterest;
  conclusion.totalYear = totalYear;
  conclusion.payPerMonth = payPerMonth;//?
  conclusion.tabHeader = tabHeader;
  conclusion.tabBody = tabBody;

  conclusion.payPerMonth = parseFloat(conclusion.payPerMonth).toFixed(2);
  conclusion.totalInterest = parseFloat(conclusion.totalInterest).toFixed(2);

  return conclusion;
}


function modifyTabHeader(isCommercialLoan, isAccumulation){
  if (isCommercialLoan && isAccumulation){
    let result = ['期数', '总还款金额', '商贷还款', '公积金还款'];
    return result;
  }else{
    let result = ['期数', '月供总额', '月供本金', '月供利息', '剩余本金'];
    return result;
  }

}

// module.exports = {
//   calculateEqualPrincipalAndInterest : calculateEqualPrincipalAndInterest,
//   calculateEqualPrincipal : calculateEqualPrincipal,
//   calculateEqualPrincipalAndInterest1 : calculateEqualPrincipalAndInterest1,
//   calculateEqualPrincipal1 : calculateEqualPrincipal1,
//   calculateEqualPrincipalAndInterestApart : calculateEqualPrincipalAndInterestApart,
//   calculateEqualPrincipalApart : calculateEqualPrincipalApart,
//   calculateEqualPrincipalAndInterestApart2 : calculateEqualPrincipalAndInterestApart2,
//   calculateEqualPrincipalApart2 : calculateEqualPrincipalApart2,
//   buildParameter: buildParameter,
//   calculate: calculate,
// }
export {
  calculateEqualPrincipalAndInterest,
  calculateEqualPrincipal,
  calculateEqualPrincipalAndInterest1,
  calculateEqualPrincipal1,
  calculateEqualPrincipalAndInterestApart,
  calculateEqualPrincipalApart,
  calculateEqualPrincipalAndInterestApart2,
  calculateEqualPrincipalApart2,
  buildParameter,
  calculate
}



